<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 幕小铺科技（大连）有限公司 [ http://www.XXX.com ]
// +----------------------------------------------------------------------
// | 2020/03/14 jason wang
// +----------------------------------------------------------------------
namespace app\erp\logic;
use think\facade\Hook;
use think\DB;
use util\Tree;
use think\facade\Debug;
use think\facade\Cache;
class ServiceClasslogic{

	/**
     * 分类列表
     * @param  int $user_id  操作用户ID
     * @author shiqiang
     * @return mixed
     */
        public function service_class_list($user_id){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
				'user_id' => $user_id,
            ];
            /********行为参数赋值（*）********/
     
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
			$list = model('ErpServiceClass') -> getClass();
            //第一步 构造数据
            $items = array();
            foreach($list as $value){
                $items[$value['id']] = $value;
            }
            //第二部 遍历数据 生成树状结构
            $tree = array();
            foreach($items as $key => $value){
                $items[$key] = [];
                $items[$key]['id'] 			= (int)$value['id'];
                $items[$key]['pid'] 		= (int)$value['pid'];
                $items[$key]['title'] 		= (string)$value['title'];
                $items[$key]['sort'] 		= (int)$value['sort'];
                $items[$key]['code'] 		= (string)$value['code'];
                $items[$key]['classImg']	= (string)$value['class_img'];
                $items[$key]['level']		= (int)$value['level'];
                $items[$key]['createTime'] 	= (int)$value['create_time'];

                if(isset($items[$value['pid']])){
                    $items[$value['pid']]['childs'][] = &$items[$key];
                }else{
                    $tree[] = &$items[$key];
                }
            }
            /********业务逻辑********/
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
			DB::commit();
            return array_out(200, 'success', $tree);
            /********逻辑返回********/
			
        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    }

}